package com.innerchic.apt.base;

import com.innerchic.apt.tool.JavaForm;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

/**
 * ================================================
 * 作    者：贾恒飞 >>> 17515250730@163.com
 * 项    目：young
 * 日    期：2021/11/1 15:34
 * 包    名：com.innerchic.apt.base
 * 描    述：绑定view页面布局的代码生成器
 * Create by zFox from AndroidStudio4.2
 * ================================================
 */
public class BindViewLayoutHolderCoder {

    private final String packageName;

    private final String className;

    private final TypeElement typeElement;

    public BindViewLayoutHolderCoder(Elements baseElements, TypeElement classElement) {
        this.typeElement = classElement;
        PackageElement packageElement = baseElements.getPackageOf(typeElement);
        String packageName = packageElement.getQualifiedName().toString();
        String className = typeElement.getSimpleName().toString();
        this.packageName = packageName;
        this.className = className + "Holder";
    }

    private final Map<String, String> mVariableElementMap = new HashMap<>();

    public void putElement(String id, String type) {
        mVariableElementMap.put(id, type);
    }

    /**
     * 创建Java代码
     */
    public String generateJavaCode() {
        //截取三段包名
        String[] split = packageName.split("\\.");
        String basePackageName = split[0] + "." + split[1] + "." + split[2];
        //开始编写代码
        StringBuilder builder = new StringBuilder();
        builder.append("package ").append(packageName).append(";\n\n");
        builder.append("import android.widget.*;\n");
        builder.append("import android.view.*;\n");
        builder.append("import ").append(basePackageName).append(".R;\n");
        builder.append("import androidx.annotation.NonNull;\n");
        builder.append("import com.innerchic.mapper.*;\n");
        builder.append('\n');
        builder.append("public class ").append(className).append(" extends androidx.recyclerview.widget.RecyclerView.ViewHolder");
        builder.append(" {\n");

        //属性
        generateMethods(builder);

        //构造方法
        builder.append("\n");
        builder.append("public ").append(className).append("(View itemView) {\n");
        //.append("com.focus.yang_app.res_find.ResFind.getInstance().bind(itemView,this);\n")
        builder.append("super(itemView);\n");

        generateMethodsBind(builder);

        builder.append("}\n");

        //增加类型转换方法
        builder.append("public static ").append(className).append(" format(androidx.recyclerview.widget.RecyclerView.ViewHolder holder){\n");
        builder.append("return (").append(className).append(")holder;\n");
        builder.append("}\n");

        //增加根据视图创建holder方法
        builder.append("public static ").append(className).append(" create(android.view.View view){\n");
        builder.append("return new ").append(className).append("(view);\n");
        builder.append("}\n");

        builder.append('\n');
        builder.append("}\n");

        return JavaForm.formJava(builder.toString());
    }


    private void generateMethods(StringBuilder builder) {
        Set<String> strings = mVariableElementMap.keySet();
        for (String id : strings) {
            String type = mVariableElementMap.get(id);
            builder.append("public ").append(type).append(" ").append(id).append(";\n");
        }
    }

    private void generateMethodsBind(StringBuilder builder) {
        Set<String> strings = mVariableElementMap.keySet();
        for (String id : strings) {
            String type = mVariableElementMap.get(id);
            builder.append(id).append("=(").append(type).append(") itemView.findViewById(R.id.").append(id).append(");\n");
        }
    }

    public String getProxyClassFullName() {
        return packageName + "." + className;
    }

    public TypeElement getTypeElement() {
        return typeElement;
    }
}
